﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  MySqlCommandExecutor
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KT.Core.Extensions;
using MySql.Data.MySqlClient;
using System.Data;
namespace KT.Framework.Database.MySql
{
    /// <summary>
    /// 基于MySql数据库的数据命令执行者
    /// </summary>
    public class MySqlDbCommandExecutor : DbCommandExecutor
    {
        /// <summary>
        /// 根据DbConnection对象实例化
        /// </summary>
        /// <param name="connection"></param>
        public MySqlDbCommandExecutor(MySqlConnection connection) : base(new MySqlDbHelper(connection))
        {}

        /// <summary>
        /// 根据数据库帮助对象实例化
        /// </summary>
        /// <param name="dbHelper"></param>
        public MySqlDbCommandExecutor(MySqlDbHelper dbHelper) : base(dbHelper) { }

        #region 执行语句并返回自增标识的值
        /// <summary>
        /// 获取插入数据后返回标识值的命令语句
        /// </summary>
        /// <returns></returns>
        protected override System.Data.Common.DbCommand GetIdentityCommand(System.Data.Common.DbCommand command)
        {
            if (command.CommandType == CommandType.Text)
            {
                command.CommandText = string.Concat(command.CommandText, ";SELECT LAST_INSERT_ID();");
                return command;
            }
            else
            {
                var cmd = command.Connection.CreateCommand();
                if (command.Transaction != null)
                    cmd.Transaction = command.Transaction;
                cmd.CommandText = "SELECT LAST_INSERT_ID()";
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = command.CommandTimeout;
                return cmd;
            }
        }
        #endregion

        #region 获取有限制的数据行集 GetRowList
        /// <summary>
        /// 获取某个表中一定数量的行数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="quantity"></param>
        /// <param name="tableName"></param>
        /// <param name="condition"></param>
        /// <param name="orderBy"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public override List<T> GetRowList<T>(int quantity, string tableName, string condition, string orderBy, params System.Data.Common.DbParameter[] pars)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.AppendFormat("SELECT * FROM {0}", this.DbHelper.QuoteIdentifier(tableName));
            if (!string.IsNullOrEmpty(condition))
            {
                buffer.AppendFormat(" WHERE {0}", condition);
            }
            if (!string.IsNullOrEmpty(orderBy))
            {
                buffer.AppendFormat(" ORDER BY {0}", orderBy);
            }
            if (quantity > 0)
            {
                buffer.AppendFormat(" LIMIT {0}", quantity);
            }
            return this.DbHelper.ExecuteDbObjectList<T>(buffer.ToString(), pars);
        }
        #endregion
    }
}
